﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.actions {
    import Engine.Helpers.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Engine.*;

    public class ActionClearWilderness extends ActionPickup {

        private const FADE_LENGTH:int = 1;
        private const WALK_ANIM:String = "static";
        private const MAX_DISTANCE_TO_WALK:int = 10;
        private const WALK_DURATION:Number = 2;

        public function ActionClearWilderness(_arg1:NPC, _arg2:int, _arg3:MapResource){
            super(_arg1, _arg2, _arg3);
        }
        override protected function onAppear():void{
            super.onAppear();
            m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                m_npc.alpha = 0;
            }));
        }
        override protected function onTravel():void{
            super.onTravel();
            if (((getTarget()) && (!(isFirstTarget())))){
                this.walkOrFade();
            };
        }
        protected function walkOrFade():void{
            var _local1:Vector3 = getTarget().getPosition().subtract(m_npc.getPosition());
            if (_local1.dot(_local1) < this.MAX_DISTANCE_TO_WALK){
                this.walkTravel();
            } else {
                this.fadeTravel();
            };
        }
        protected function walkTravel():void{
            var posA:* = null;
            var posB:* = null;
            var stepOne:* = null;
            var posAWalkable:* = false;
            var posBWalkable:* = false;
            var bothWalkableAndShouldSwitch:* = false;
            var diff:* = getTarget().getPosition().subtract(m_npc.getPosition());
            if (((!((diff.x == 0))) && (!((diff.y == 0))))){
                posA = new Vector3(getTarget().getPosition().x, m_npc.getPosition().y);
                posB = new Vector3(m_npc.getPosition().x, getTarget().getPosition().y);
                stepOne = posA;
                posAWalkable = (Global.world.getCollisionMap().getObjectsByPosition(posA.x, posA.y).length == 0);
                posBWalkable = (Global.world.getCollisionMap().getObjectsByPosition(posB.x, posB.y).length == 0);
                bothWalkableAndShouldSwitch = ((((posAWalkable) && (posBWalkable))) && ((MathUtil.randomIncl(1, 0) == 1)));
                if (((!(posAWalkable)) && (!(posBWalkable)))){
                    this.fadeTravel();
                    return;
                };
                if (((bothWalkableAndShouldSwitch) || (!(posAWalkable)))){
                    stepOne = posB;
                };
                m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                    m_npc.animation = WALK_ANIM;
                }), new ActionTranslate(m_npc, this.WALK_DURATION, stepOne), new ActionTranslate(m_npc, this.WALK_DURATION, getTarget().getPosition()));
            } else {
                m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                    m_npc.animation = WALK_ANIM;
                }), new ActionTranslate(m_npc, this.WALK_DURATION, getTarget().getPosition()));
            };
        }
        protected function fadeTravel():void{
            m_npc.getStateMachine().addActions(new ActionTween(m_npc, ActionTween.TO, this.FADE_LENGTH, {alpha:0}));
        }
        override protected function onPickup():void{
            if (getTarget()){
                m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                    m_npc.setPosition(getTarget().getPosition().x, getTarget().getPosition().y);
                    m_npc.conditionallyReattach();
                    getTarget().unlock();
                }));
            };
            setNextState(STATE_TRAVEL);
            if (!getTarget()){
                return;
            };
            var clearTimeInSec:* = Global.gameSettings.getNumber("actionBarWildernessClear");
            var randomDir:* = MathUtil.randomIncl(Constants.DIRECTION_NW, Constants.DIRECTION_NE);
            m_npc.setDirection(randomDir);
            m_npc.setState(m_npc.getState());
            Sounds.play("lumberjack");
            m_npc.getStateMachine().addActions(new ActionFn(m_npc, function ():void{
                var _local1:* = (getTarget() as Wilderness);
                m_npc.animation = _local1.getPickupAnim();
            }));
            if (m_npc.alpha < 1){
                m_npc.getStateMachine().addActions(new ActionAnimationEffect(m_npc, EffectType.POOF));
            };
            var target:* = getTarget();
            if (!target.canClearFromMap()){
                target.unlock();
                Global.world.citySim.pickupManager.clearQueue(m_npc.getItemName());
                Global.world.citySim.npcManager.removePickupNPC(m_npc.getItemName());
                return;
            };
            m_npc.getStateMachine().addActions(new ActionProgressBar(m_npc, target, target.getActionText(), clearTimeInSec));
        }

    }
}//package Classes.actions 
